﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using CPPEI.Coolzon.Common;
using System.Xml;

namespace CPPEI.Coolzon.Metadata.ParserV1
{
    public class PartLibXmlParser : IPartLibXmlParser
    {
        public PartLib Load(XmlDocument xDoc)
        {
            if (xDoc == null) return null;
            try
            {
                XmlNode xPartLib = xDoc.SelectSingleNode("PartLib");
                if (xPartLib == null) return null;

                PartLib partLib = new PartLib();

                partLib.Name = xPartLib.GetXAttributeValue("Name");
                partLib.Caption = xPartLib.GetXAttributeValue("Caption");
                partLib.Version = Version.Parse(xPartLib.GetXAttributeValue("Version"));

                List<PartLibItem> partLibItems = new List<PartLibItem>();
                XmlNodeList xNodePartLibItems = xPartLib.SelectNodes("PartItem");
                foreach (XmlNode xPartLibItem in xNodePartLibItems)
                {
                    PartLibItem partLibItem = new PartLibItem();
                    partLibItem.Name = xPartLibItem.GetXAttributeValue("Name");
                    partLibItem.Caption = xPartLibItem.GetXAttributeValue("Caption");
                    partLibItem.Type = xPartLibItem.GetXAttributeValue("Type");
                    partLibItem.Assembly = xPartLibItem.GetXAttributeValue("Assembly");
                    partLibItem.GroupName = partLib.Caption;
                    partLibItems.Add(partLibItem);
                }

                partLib.PartList = partLibItems;

                return partLib;
            }
            catch
            {
                return null;
            }
        }
    }
}
